//http://web.i-fortune.com.tw/stock/tool/engine.cfm?engine=1
//http://web.i-fortune.com.tw/stock/company/evaluator.cfm?scode=2002&cat=3&period=1

function ObjStkItu(parent_) {	
	this.parent_ = parent_;
	this.id = parent_.id;
		
	this.rtar = 0;
	this.roe = 0;
	this.rgr = 0;		//營收成長率(c1)
	this.ngr = 0;		//淨利成長率(c1)
	this.fcfgr = 0;	//現金流量成長率(c1)
	this.dr = 0;		//負債比率(c2)
	this.fa = 0;	 	//固定資產(c2)
	this.atr = 0;		//資產周轉率(c4)
	this.itr = 0;		//存貨周轉率 (c4)
	this.gptr = 0;	//應收帳款周轉率 (c4)
	this.per = 0;		//本益比 (c5)
	this.psr = 0;		//每股營收比 (c5)
	
	this.press = 0;
	this.support = 0;
	
	this.maxCat = 5;
	this.interval = 0;
	this.retry = 0;
}
ObjStkItu.prototype.Fetch = function(cat) {
	if (cat <= 5) {
		url_ = "http://web.i-fortune.com.tw/stock/company/evaluator.cfm?scode="+this.id+"&cat="+cat+"&period=1";
	} else if (cat == 6) {
		url_ = "http://web.i-fortune.com.tw/stock/company/sup_res.cfm?scode="+this.id+"&per=2";
	} 
	this.curCat = cat;
	//nop();
	
	if (this.interval) {
		clearTimeout( this.interval );
		this.interval = 0;
		++this.retry;
	}
	if (this.retry > 3) {
		this.parent_.Filter();
		alert("give up");
		return;
	}
	
	_IG_FetchContent(url_, _IG_Callback(Fetch_, cat, this), { refreshInterval: 300});
	dbgMsg(url_);
	
//	var scope = this;
//	this.interval = setTimeout( function(){ scope.Fetch(scope.curCat) }, 6000);	
	
	function Fetch_(response, cat, itu) {
		clearTimeout( itu.interval );
		itu.interval = 0;
		itu.retry = 0;
		dbgMsg(url_+" return");
		if (cat <= itu.maxCat) {			
			itu.parse(response, cat);
		}
		if (cat == itu.maxCat) {
			itu.parent_.Filter();
			return;
		}
		
		itu.Fetch(++cat);
	}
}
ObjStkItu.prototype.parse = function(content, cat) {
	var enuSrh = new ObjEnumSearch(content);
	
	if (cat == 1) {
		this.rgr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.ngr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.fcfgr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
	} else if (cat == 2) {
		this.dr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.fa = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
	} else if (cat == 3) {
		this.rtar = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.roe = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		//alert(this.rtar + ", " + this.roe);
	} else if (cat == 4) {
		this.atr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.itr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.gptr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
	} else if (cat == 5) {
		this.per = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
		this.psr = CountStars(enuSrh.substring_("<td class=\"C star\">", 0, "</td>", 0));
	}	else if (cat == 6) {
		this.press = enuSrh.substring_("<span class=\"press\">", 3, "</span>", 0);
		if (this.press == ' ')	this.press = "0";
		this.support = enuSrh.substring_("<span class=\"support\">", 3, "</span>", 0);
		if (this.support == ' ') this.support = "0";
		//alert("press/support:"+this.press+"/"+this.support);
	}
}

function CountStars(str) {
	// the solid star unicode is 9733
	stars = 0;
	for (var i=0; i < str.length; i++) {
		if (str.charCodeAt(i) == 9733)
			++stars;
	}	
	return stars;
}

//====================================================
function ObjStkDbIftu(g_var) {
}
ObjStkDbIftu.prototype.Create = function() {
	var url_ = this.g_var.url_iftu;
	
	_IG_FetchContent(url_, _IG_Callback(Create_, this),  { refreshInterval: 300});
	
	function Create_(content, stkdb) {
	}
}
